package tests;

import java.util.Date;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.TestingAuthenticationToken;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import repositories.PagoRepository;
import security.LoginService;
import services.ComunidadDeVecinosService;
import services.PagoService;
import utilities.PopulateDatabase;
import domain.ComunidadDeVecinos;
import domain.Pago;
@Transactional
@ContextConfiguration(locations = { "classpath:spring/datasource.xml",
"classpath:spring/config/packages.xml" })
@RunWith(SpringJUnit4ClassRunner.class)
public class CU35PagarPagoAdministrador {
	

	@Autowired
	private ComunidadDeVecinosService comunidadDeVecinosService;

	@Autowired
	private PagoService pagoService;
	
	@Autowired
	private PagoRepository pagoRepository;


	@Autowired
	private LoginService loginService;

	public void authenticate(String username) {
		UserDetails userDetails;
		TestingAuthenticationToken authenticationToken;
		SecurityContext context;
		userDetails = loginService.loadUserByUsername(username);
		authenticationToken = new TestingAuthenticationToken(userDetails, null);
		context = SecurityContextHolder.getContext();
		context.setAuthentication(authenticationToken);
	}

	@Before
	public void setUp() {
		PopulateDatabase.main(null);
	}
	
	@SuppressWarnings("deprecation")
	@Test
	public void testPagarPagoAdministrador(){
		authenticate("admin1");
		ComunidadDeVecinos cv;
		Pago pago;
		cv = comunidadDeVecinosService.findOne(10);
		pago = pagoService.create(cv);
		pago.setImporte(30.58);
		pago.setRazonSocial("Asytec");
		pago.setFecha(new Date("01/01/2014 20:30"));
		pago = pagoRepository.save(pago);
		
		pagoService.pagar(pago.getId());
		
		
	}

		

	
	@Test(expected = IllegalArgumentException.class)

	public void testPagarPagoVecino() {
		authenticate("12345678K");
		Pago pago;
		ComunidadDeVecinos cv;
		
		cv = comunidadDeVecinosService.findOne(10);
		pago = pagoService.create(cv);
		pago.setImporte(230.58);
		pago.setRazonSocial("Endesa");
		pago = pagoRepository.save(pago);
		
		pagoService.pagar(pago.getId());

		

	}
	

	@Test(expected = IllegalArgumentException.class)

	public void testPagarPagoSuperiorAFondo() {
		authenticate("admin1");
		Pago pago;
		ComunidadDeVecinos cv;
		
		cv = comunidadDeVecinosService.findOne(10);
		pago = pagoService.create(cv);
		pago.setImporte(2030.58);
		pago.setRazonSocial("Endesa");
		pago = pagoRepository.save(pago);
		
		pagoService.pagar(pago.getId());

		

	}
	
	@Test
	public void testComprobarFondo() {
		authenticate("admin1");
		double fondoAnterior;
		double fondoPosterior;
		Pago pago;
		ComunidadDeVecinos cv;
		
		cv = comunidadDeVecinosService.findOne(10);
		pago = pagoService.create(cv);
		pago.setImporte(30.58);
		pago.setRazonSocial("Endesa");
		pago = pagoRepository.save(pago);

		
		fondoAnterior = pago.getComunidadDeVecinos().getFondo();

		pagoService.pagar(pago.getId());

		pago = pagoService.findOne(pago.getId());
		fondoPosterior = pago.getComunidadDeVecinos().getFondo();

		Assert.isTrue(fondoPosterior == fondoAnterior - pago.getImporte());

	}
	
	@Test(expected = IllegalArgumentException.class)

	public void testPagarPagoYaPagadoAdministrador(){
		authenticate("admin1");
		ComunidadDeVecinos cv;
		Pago pago;
		cv = comunidadDeVecinosService.findOne(10);
		pago = pagoService.create(cv);
		pago.setImporte(30.58);
		pago.setRazonSocial("Asytec");
		pago = pagoRepository.save(pago);
		
		pagoService.pagar(pago.getId());
		
		pagoService.pagar(pago.getId());
		
		
	}
	
	@SuppressWarnings("deprecation")
	public void testPagarPagoFuturoAdministrador(){
		authenticate("admin1");
		ComunidadDeVecinos cv;
		Pago pago;
		cv = comunidadDeVecinosService.findOne(10);
		pago = pagoService.create(cv);
		pago.setImporte(30.58);
		pago.setRazonSocial("Asytec");
		pago.setFecha(new Date("01/01/2015 20:30"));
		pago = pagoRepository.save(pago);
		
		pagoService.pagar(pago.getId());
		
		pagoService.pagar(pago.getId());
		
		
	}
	


		
	



}
